package com.clj.blesample.menu;

import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.clj.blesample.databinding.ActivityChangeIpBinding;
import com.clj.blesample.entity.AppConstant;

public class ChangeIpActivity extends AppCompatActivity {
    private ActivityChangeIpBinding mBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityChangeIpBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        setSupportActionBar(mBinding.toolbar);
        getSupportActionBar().setTitle("更改服务器地址");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        mBinding.inputUdpPort.getEtContent().setInputType(InputType.TYPE_CLASS_NUMBER);

        mBinding.inputUdpIp.setContent(AppConstant.getIP());
        mBinding.inputUdpPort.setContent(AppConstant.getPort());
        mBinding.inputFileUpload.setContent(AppConstant.getFileUrl());
        mBinding.inputUrl.setContent(AppConstant.getUrl());
        mBinding.tvModify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String ip = mBinding.inputUdpIp.getContent();
                String port = mBinding.inputUdpPort.getContent();
                String fileUrl = mBinding.inputFileUpload.getContent();
                String url = mBinding.inputUrl.getContent();
                if (TextUtils.isEmpty(ip)) {
                    Toast.makeText(ChangeIpActivity.this, "请输入udp服务器ip", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(port)) {
                    Toast.makeText(ChangeIpActivity.this, "请输入udp服务器端口", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(fileUrl)) {
                    Toast.makeText(ChangeIpActivity.this, "请输入文件上传地址地址", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(url)) {
                    Toast.makeText(ChangeIpActivity.this, "请输入http接口地址", Toast.LENGTH_SHORT).show();
                    return;
                }
                AppConstant.setIP(ip);
                AppConstant.setPort(port);
                AppConstant.setFileUrl(fileUrl);
                AppConstant.setUrl(url);
                Toast.makeText(ChangeIpActivity.this, "修改成功！", Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}
